﻿var checklist = {
    page: 1,
    init: function() {
        var that = this;
        that.dateInit();



        that.doData();
        that.initCheckBox();
        that.initCount();

        $("#che-query").click(function() {
            // that.doSearch();
            // 重置分页
            that.page = 1;
            that.doData();
        });
    },

    dateInit: function() {
        $('.datepicker').datetimepicker({
            language: 'zh-CN',
            weekStart: 1,
            todayBtn: 1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            minView: 2,
            forceParse: 0,
            format: 'yyyy-mm-dd hh:ii'
        });
    },
    format: function(nS) {
        //return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');
        if (!nS) return null;
        var date = new Date(nS * 1000);
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var d = date.getDate();
        var hours = date.getHours();
        var minutes = date.getMinutes();
        var seconds = date.getSeconds();
        return year + '-' + month + '-' + d + ' ' + hours + ':' + minutes;
    },
    // 运单状态的过滤器
    stataFilter: function(stat) {
        return {
            '1': '普通运单',
            '10': '未取货'
        }[stat];
    },
    /**
     * 页面数据渲染
     * @return {[type]} [description]
     */
    doData: function() {
        var that = this;
        // 获取搜素参数
        var wuliuname = $.trim($("#wuliuname").val())
        state_bid_time1 = $.trim($("#State_Bid_Time1").val()) == '' ? '' : new Date($.trim($("#State_Bid_Time1").val())).getTime(),
            state_bid_time2 = $.trim($("#State_Bid_Time2").val()) == '' ? '' : new Date($.trim($("#State_Bid_Time2").val())).getTime(),
            end_bid_time1 = $.trim($("#End_Bid_Time1").val()) == '' ? '' : new Date($.trim($("#End_Bid_Time1").val())).getTime(),
            end_bid_time2 = $.trim($("#End_Bid_Time2").val()) == '' ? '' : new Date($.trim($("#End_Bid_Time2").val())).getTime();



        // 请求数据并渲染列表
        $.ajax({
            url: COMMONURL + "api/ReportBase/GetPageList?token=" + $.cookie('token'),
            type: "post",
            data: {
                wuliuname: wuliuname,
                consignee_time1: state_bid_time1,
                consignee_time2: state_bid_time2,
                pickup_time1: end_bid_time1,
                pickup_time2: end_bid_time2,
                "page": that.page,
                "pagesize": 20,
                // 表示未结账
                state: 1
            },
            dataType: "json"
        }).done(function(redata) {
            if (redata.recode == '0000' && redata.mod.Items.length > 0) {
                var compiled = _.template($("#ChecklisttTpl").html());
                var odata = redata.mod.Items;
                for (var i = 0, length = odata.length; i < length; i++) {
                    odata[i]._pickup_time = that.format(odata[i]._pickup_time);
                    odata[i]._start_bid_time = that.format(odata[i]._start_bid_time);
                    odata[i]._report_time = that.format(odata[i]._report_time);
                }

                $("#ChecklisttabCon").html(compiled(redata.mod));
                $("#che-page").show().jqPaginator({
                    //totalPages: Math.ceil(redata.mod.TotalNum/20),
                    totalPages: redata.mod.TotalPageCount,
                    visiblePages: 5,
                    currentPage: that.page,
                    first: '<li class="first"><a href="javascript:void(0);">首页<\/a><\/li>',
                    prev: '<li class="prev"><a href="javascript:void(0);"><i class="arrow arrow2"><\/i>上一页<\/a><\/li>',
                    next: '<li class="next"><a href="javascript:void(0);">下一页<i class="arrow arrow3"><\/i><\/a><\/li>',
                    last: '<li class="last"><a href="javascript:void(0);">末页<\/a><\/li>',
                    page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
                    onPageChange: function(num, type) {

                        that.page = num;
                        if (type == 'change') {
                            that.doData();
                        }
                    }
                });

            } else {

                var compiled = _.template($("#ChecklisttTpl").html());
                var odata = redata.mod.Items;
                $("#ChecklisttabCon").html(compiled(redata.mod));
                $("#che-page").hide();
            }
        });
    },
    initCheckBox: function() {
        $('#ChecklisttabCon')
            .on('click', '.input-all', function() {
                $('.input-one').attr('checked', $('.input-all').prop('checked'));
            })
            .on('click', '.input-one', function(ev) {
                $('.input-all').attr('checked',
                    $('.input-one:checked').length == $('.input-one').length);
            });
    },
    initCount: function() {
        var that = this;
        $('#che-count').on('click', function() {
            // 存放需要结算的id
            var ids = [];
            // 收集id
            //debugger;
            $('.input-one').each(function() {
                    if($(this).closest('tr').find('.input-one:checked').length<=0)
                        return;
                    var id = $(this).closest('tr').find('.input-one:checked').val();
                    //alert($(this).closest('tr').find('.input-one:checked').val());
                    ids.push(id);
                })
                // 判断是否有选中的需要结算的账单
            if (ids.length <= 0) {
                alert('请选择需要结算的账单');
                return;
            }
            // 转换成字符串
            ids = ids.join(',');
            //alert(ids);
            $.ajax({
                url: COMMONURL + "api/ReportBase/OperationReport?token=" + $.cookie('token'),
                type: "post",
                data: {
                    ids: ids
                },
                dataType: "json"
            }).done(function(redata) {
                if (redata.recode == '0000') {
                    that.doData();
                    alert('结算成功');
                     window.location.reload();
                } else {
                     alert('结算失败');
                }
            });
        })
    }
}

checklist.init();
